package com.bstek.uflo.designer.deserializer.impl;

import org.dom4j.Element;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.bstek.uflo.designer.model.Shape;
import com.bstek.uflo.designer.model.node.Join;

/**
 * @author matt.yao@bstek.com
 * @since 1.0
 */
@Service
public class JoinDeserializer extends NodeDeserializer {

	public Shape execute(Element currentElement) throws Exception {
		Join join = new Join();
		super.deserializeCommonAttribute(join, currentElement);
		String multiplicity = currentElement.attributeValue("multiplicity");
		String percentMultiplicity = currentElement.attributeValue("percent-multiplicity");
		if (StringUtils.hasText(multiplicity)) {
			join.setMultiplicityType("multiplicity");
			join.setMultiplicity(Integer.parseInt(multiplicity));
			join.setUseMultiplicity(true);

		} else if (StringUtils.hasText(percentMultiplicity)) {
			join.setMultiplicityType("percentMultiplicity");
			join.setPercentMultiplicity(Integer.parseInt(percentMultiplicity));
			join.setUseMultiplicity(true);
		}

		return join;
	}

	public boolean support(String name) {
		return name.equals("join") ? true : false;
	}

}
